import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Register from "../components/register/Register.vue";
import Login from "../components/login/Login.vue"
import Products from '../components/public/Products.vue';
import AdminDashboard from "../views/AdminDashboard.vue";
import AdminHome from "../views/admin/Home.vue"
import AllProducts from "../views/admin/AllProducts.vue";
import AllCustomers from "../views/admin/AllCustomers.vue";
import AllSellers from "../views/admin/AllSellers.vue";
import AllOrders from "../views/admin/AllOrders.vue";
import UserManagement from "../views/admin/UserManagement.vue";
import AdminProfile from "../views/admin/AdminProfile.vue";
import SellerDashboard from "../views/SellerDashboard.vue";
import SellerHome from "../views/seller/Home.vue"
import CustomerDashboard from "../views/CustomerDashboard.vue";
import CustomerHome from "../views/customer/Home.vue"
import Profile from "../views/customer/Profile.vue";
import Cart from "../views/customer/Cart.vue"
import Orders from "../views/customer/Orders.vue";
import ProductManagement from "../views/seller/ProductManagement.vue";
import OrderManagement from "../views/seller/OrderManagement.vue";
import Logfile from "../views/seller/Logfile.vue";
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "/login"  // 指定初始页面为客户登录页面
  },
  {
    path: "/login",
    name: 'Login',
    component: Login,
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/products',
    name: 'Products',
    component: Products,
  },
  {
    path: "/admin",
    component: AdminDashboard,
    meta: { requiresAuth: true, role: 'ADMIN' },
    children: [
      {
        path: '',
        name: 'AdminHome',
        component:AdminHome,
      },
      {
        path: 'products',
        name: 'AllProducts',
        component:AllProducts,
      },
      {
        path: 'users',
        name: 'UserManagement',
        component:UserManagement,
      },
      {
        path: 'customers',
        name: 'AllCustomers',
        component:AllCustomers,
      },
      {
        path: 'sellers',
        name: 'AllSellers',
        component:AllSellers,
      },
        {
          path: 'orders',
          name: 'AllOrders',
          component:AllOrders,
        },
        {
          path: 'profile',
          name: 'AdminProfile',
          component:AdminProfile,
        },
    ]
  },
  {
    path: "/seller",
    component: SellerDashboard,
    meta: { requiresAuth: true, role: 'SELLER' },
    children: [
      {
        path: '',
        name: 'SellerHome',
        component: SellerHome,
      },
      {
        path: 'products',
        name: 'SellerProducts',
        component: Products,
      },
      {
        path: 'product-management',
        name: 'ProductManagement',
        component: ProductManagement,
      },

      {
        path: 'order-management',
        name: 'OrderManagement',
        component: OrderManagement,
      },
      {
        path: 'logfile',
        name: 'Logfile',
        component: Logfile,
      }
    ]
  },
  {
    path: "/customer",
    component: CustomerDashboard,
    meta: { requiresAuth: true, role: 'CUSTOMER' },
    children: [
      {
        path: '',
        name: 'CustomerHome',
        component: CustomerHome,
      },
      {
        path: 'products',
        name: 'CustomerProducts',
        component: Products,
      },

      {
        path: 'cart',
        name: 'Cart',
        component: Cart,
      },
      {
        path: 'orders',
        name: 'Orders',
        component: Orders,
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile,
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

// 添加全局导航守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = sessionStorage.getItem('isLoggedIn'); // 检查登录状态
  const userRole = sessionStorage.getItem('userRole'); // 检查用户角色
  console.log(from.path)
  // 如果目标页面需要登录且用户未登录，或者用户角色不匹配，则跳转到登录页面
  if (to.meta.requiresAuth==true && !isLoggedIn) {
    next({ name: 'Login' });
  } else if (to.meta.requiresAuth==true && to.meta.role && userRole !== to.meta.role) {
    // 如果角色不匹配，则跳转到默认页面或者拒绝访问
    next({ name: 'Login' });
  } else {
    next();
  }
});

export default router;
